﻿using System;
using System.IO;
using System.Linq;
using System.Web.Hosting;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xenta;

namespace IisHost.UI
{
    public partial class Maintenance : Page
    {
        protected override void OnLoadComplete(EventArgs e)
        {
            if(!IsPostBack)
            {
                if(Context.User == null || !Context.User.Identity.IsAuthenticated)
                {
                    pnlContent.Visible = false;
                    btnLogout.Visible = false;
                    pnlLogin.Visible = true;

                    ctlLogin.BindData();
                }
                else
                {
                    pnlContent.Visible = true;
                    btnLogout.Visible = true;
                    pnlLogin.Visible = false;

                    ctlPassword.BindData();
                    ctlRuntime.BindData();
                    ctlDatabase.BindData();
                    ctlLog.BindData();
                    
                    var appDataDir = new DirectoryInfo(HostingEnvironment.MapPath("~/App_Data"));
                    var infrastructureDirs = appDataDir.GetDirectories().Select(x => x.Name);

                    lstInfrastructures1.DataSource = infrastructureDirs;
                    lstInfrastructures1.DataBind();

                    lstInfrastructures2.DataSource = infrastructureDirs;
                    lstInfrastructures2.DataBind();
                }
            }
            base.OnLoadComplete(e);
        }

        protected void lstInfrastructures2_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var item = e.Item;
            if(item.ItemType != ListItemType.Item && item.ItemType != ListItemType.AlternatingItem)
                return;
            var ctlNHibernate = item.FindControl("ctlNHibernate")
                as NHibernateControl;
            var ctlPlugins = item.FindControl("ctlPlugins")
                as PluginControl;
            ctlNHibernate.Infrastructure = e.Item.DataItem as String;
            ctlNHibernate.BindData();
            ctlPlugins.Infrastructure = e.Item.DataItem as String;
            ctlPlugins.BindData();
        }

        protected void OnCommand(object sender, CommandEventArgs e)
        {
            switch(e.CommandName)
            {
                case "Logout":
                    {
                        Context.Session.Abandon();
                        FormsAuthentication.SignOut();
                        Response.Redirect("~/");
                    }
                    break;
                case "InfrastructureDelete":
                    {
                        string appDataDir = HostingEnvironment.MapPath("~/App_Data");
                        string infrastructureName = e.CommandArgument as String;
                        string infrastructureDir = Path.Combine(appDataDir, infrastructureName);
                        Directory.Delete(infrastructureDir, true);
                        Response.Redirect("~/");
                    }
                    break;
                case "InfrastructureCreate":
                    {
                        string appDataDir = HostingEnvironment.MapPath("~/App_Data");
                        string infrastructureName = txtInfrastructure_Name.Text;
                        infrastructureName = infrastructureName.Trim();
                        string infrastructureDir = Path.Combine(appDataDir, infrastructureName);
                        Directory.CreateDirectory(infrastructureDir);
                        File.Copy(Path.Combine(appDataDir, InfrastructureImpl.NH_CFG_FILENAME),
                            Path.Combine(infrastructureDir, InfrastructureImpl.NH_CFG_FILENAME));
                        File.Copy(Path.Combine(appDataDir, InfrastructureImpl.PLUGINS_FILENAME),
                            Path.Combine(infrastructureDir, InfrastructureImpl.PLUGINS_FILENAME));
                        Response.Redirect("~/");
                    }
                    break;
            }
        }
    }
}